package cn.yunyichina.provider.hymed.service.impl;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.common.ResponseEnum;
import cn.yunyichina.provider.hymed.constant.NeusoftFunctionId;
import cn.yunyichina.provider.hymed.model.bo.*;
import cn.yunyichina.provider.hymed.model.domain.HymedPaymentSettingDO;
import cn.yunyichina.provider.hymed.model.vo.SmsCodeRequestVO;
import cn.yunyichina.provider.hymed.model.vo.SmsCodeValidateRequestVO;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftBaseService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 短信服务
 * Created by Administrator on 2017/11/10.
 */
@Service
public class SmsServiceImpl extends NeusoftBaseService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private final NeusoftService neusoftService;

    @Autowired
    public SmsServiceImpl(NeusoftService neusoftService) {
        this.neusoftService = neusoftService;
    }

    /**
     * 一卡通短信验证码发送接口
     * yyt.hymed.sms.code.send
     * @param data
     * @param sessionId
     * @return
     */
    public Response sendSmsCode(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通短信验证码发送接口 --->method:sendSmsCode,data:" + data + "！");
        SmsCodeRequestVO requestVO = JsonUtils.parseObject(data, SmsCodeRequestVO.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用4.1.3	用户是否存在验证
        SmsCodeGetRequestBO requestBO = new SmsCodeGetRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.GET_SMS_CODE);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setBusinesscode(requestVO.getBusinessCode());
        requestBO.setMobile(requestVO.getPatMobile());
        requestBO.setSmsMessage("验证码");

        SmsCodeGetResponseBO responseBO = neusoftService.doExecute(requestBO, SmsCodeGetResponseBO.class);
        logger.info("neusoftService SmsCodeGetResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用短信验证码获取接口失败");
            return response;
        }else{
            response.setResultCodeAndMessage("0",responseBO.getMessage());
        }

        return response;
    }

    /**
     * 一卡通短信验证码验证接口
     * yyt.hymed.sms.code.validate
     * @param data
     * @param sessionId
     * @return
     */
    public Response validateSmsCode(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通短信验证码验证接口 --->method:validateSmsCode,data:" + data + "！");
        SmsCodeValidateRequestVO requestVo = JsonUtils.parseObject(data, SmsCodeValidateRequestVO.class);
        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用 4.1.17	验证短信验证码接口
        SmsCodeValidateRequestBO smsRequestBO = new SmsCodeValidateRequestBO();
        smsRequestBO.setFunction_id(NeusoftFunctionId.VALIDATE_SMS_CODE);
        smsRequestBO.setPortalLoginId(requestVo.getLoginName());
        smsRequestBO.setMobile(requestVo.getPatMobile());
        smsRequestBO.setSmsCode(requestVo.getSmsCode());

        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        smsRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        smsRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        smsRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        SmsCodeValidateResponseBO smsResponseBO = neusoftService.doExecute(smsRequestBO,SmsCodeValidateResponseBO.class);
        if (smsResponseBO == null || !smsResponseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (smsResponseBO != null && StringUtils.isNotBlank(smsResponseBO.getMessage())) ? smsResponseBO.getMessage() : "验证码验证失败");
            return response;
        }
        response.setResultCodeAndMessage(ResponseEnum.Common.SUCCESS.getCode(),"验证成功");
        return response;
    }
}
